from locust import HttpUser, TaskSet, task, between
from locust.clients import HttpSession
import json
import hashlib  # python md5加密


# web性能测试

class UserBehavior(TaskSet):
    host = 'http://192.168.1.144/rcm/a'

    def login(self):
        loginUrl = '/login'

        self.header = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Encoding": "gzip, deflate"
        }
        # 密码ras加密，暂时没用
        username = "6bcce5f671fd3f27672c13feef54c8f550aeab495e1d04fba86120d2e99c1beed54258a70145d0371c3a738bb2dd1d9232fed6235f9f2e8f2722c974caf42fe4777c4d51237e3274473d5188367058931eafae68e5b9d70c74824d864467117dfb03b9b818d4646772adec9eadbf7044b43e329e44d38dff4e15459198237ecc "

        password = "7b54901eaa0e6b69ff603cb02a1aef7749567eeb5a61e5038e86eabccb7ba45d34e742572d70bb80ed8bb7e19f32fa9b9d93874b5419cb4512fe119c50878e4f126e53f67434c8a0441a57a65b6ea7df01e40352dfb95bc4e319f516ab42f662b569896f3d48e030793c65d31b4824cb55d8eb8fafd1c0092f8d1b47b0f125b2"

        accountSuitId = "fa80c8f8b9ee43a79fdfa5ddf8d29807"

        loginType = "1"

        body = {
            "username": username,
            "password": password,
            "accountSuitId": accountSuitId,
            "loginType": loginType
        }

        r = self.client.post(loginUrl, headers=self.header, data=body)
        print(r.text)


        if r.status_code == 200:
            print("success")
        else:
            print("fails")

    def on_start(self):
        """ 任务开始准备：只登陆一次 """
        self.login()

    @task(1)
    def my_work(self):
        print("我的首页")
        url_my = "/homepage/portlet/portlet?tabPageId=jerichotabiframe_4"

        r = self.client.get(url_my, headers=self.header)

        if r.status_code == 200:
            print("success")
        else:
            print("fails")


class User(HttpUser):
    task_set = UserBehavior
    wait_time = between(3, 5)


'''
if __name__ == "__main__":
    import os
    os.system("locust -f loginHttpSession_rcm.py --host=http://192.168.1.144/rcm/a")
'''
